﻿using Learun.Application.Base.SystemModule;
using Learun.Application.Organization;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-15 10:00
    /// 描 述：报表文件
    /// </summary>
    public class ec_report_fileBLL : ec_report_fileIBLL
    {
        private ec_report_fileService ec_report_fileService = new ec_report_fileService();

        #region 获取数据
        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<ec_report_fileEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var ProjectId = queryParam["ProjectId"]?.ToString();
                var ReportFileName = queryParam["ReportFileName"]?.ToString();
                var ReportCatalogueID = queryParam["ReportCatalogueID"]?.ToString();
                var ReportFileNameFull = queryParam["ReportFileNameFull"]?.ToString();
                var tableName = TableName<ec_report_fileEntity>(ProjectId);
                var query = Db.Queryable<ec_report_fileEntity>().AS(tableName)
                    .WhereIF(!string.IsNullOrEmpty(ReportFileName), x => x.ReportFileName.Contains(ReportFileName))
                    .WhereIF(!string.IsNullOrEmpty(ReportCatalogueID), x => x.ReportCatalogueID == ReportCatalogueID)
                    .WhereIF(!string.IsNullOrEmpty(ReportFileNameFull), x => x.ReportFileName == ReportFileNameFull)
                    .OrderByDescending(x => x.CreateTime);
                List<ec_report_fileEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                var users = Db.Queryable<UserEntity>().ToList();
                list?.ForEach(x =>
                {
                    x.CreateUserName = users.FirstOrDefault(y => y.F_UserId == x.CreateUserID)?.F_RealName;
                });
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_report_fileEntity GetEntity(string ProjectId, string keyValue)
        {
            try
            {
                return ec_report_fileService.GetEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            try
            {
                ec_report_fileService.DeleteEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_report_fileEntity entity)
        {
            try
            {
                ec_report_fileService.SaveEntity(ProjectId, keyValue, entity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 修改集合数据,新增附件数据集合
        /// <summary>
        /// <returns></returns>
        public void SaveDataAndAnnexes(string ProjectId)
        {
            try
            {
                UserInfo userInfo = LoginUserInfo.Get();
                List<AnnexesFileEntity> listAnnexesFile = new List<AnnexesFileEntity>();
                var reportFileData = GetList("{ProjectId:\"" + ProjectId + "\"}").ToList();
                foreach (var item in reportFileData)
                {
                    var fileList = new AnnexesFileService().GetList(item.FolderId).ToList();
                    item.FolderId = Guid.NewGuid().ToString();
                    for (var i = 0; i < fileList.Count; i++)
                    {
                        fileList[i].F_Id = Guid.NewGuid().ToString();
                        fileList[i].F_FolderId = item.FolderId;
                        fileList[i].F_CreateDate = Time.MySqlTime;
                        fileList[i].F_CreateUserId = userInfo.userId;
                        fileList[i].F_CreateUserName = userInfo.realName;
                        listAnnexesFile.Add(fileList[i]);
                    }
                }
                ec_report_fileService.SaveDataAndAnnexes(reportFileData, listAnnexesFile, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

    }
}
